package com.webmall.servlet.user;

import com.webmall.entity.UserEntity;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/manageHtml/admin_doUserShow")
public class doUserShow extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        int cpage=1;
        int count =2;//每一页显示多少条

        String cp = request.getParameter("cp");//获取用户指定的页面
        if (cp !=null){
            cpage=Integer.parseInt(cp);
        }

        String keyword = request.getParameter("keywords");//用户搜索时传入关键词

        int[] arr = com.webmall.service.webmall_UserDao.totalPage(count,keyword);

        //获取所有用户记录
        ArrayList<UserEntity> list=com.webmall.service.webmall_UserDao.selectAll(cpage,count,keyword);
        //放到请求对象
        request.setAttribute("userlist",list);
        request.setAttribute("tsum",arr[0]);//第一项为总记录数 第二项为页数
        request.setAttribute("pagenum",arr[1]);
        request.setAttribute("cpage",cpage);

        if (null != keyword){//搜索时的上下页修改
            request.setAttribute("searchParams","&keywords="+keyword);
        }
        //转发
        request.getRequestDispatcher("/manageHtml/admin_user.jsp").forward(request,response);
        //response.sendRedirect(request.getContextPath()+"/manageHtml/admin_user.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
